﻿using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrarySL
{
    public class Related
    {
        private System.Xml.Linq.XElement m_XElement;

        private Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sharing m_Sharing;

        public enum RelatedTypes
        {
            Complete = 0,
            Aggregated = 1
        }

        private string m_Link;
        private string m_Title;

        private Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related.RelatedTypes m_RelatedType;

        public Related(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sharing i_Sharing, System.Xml.Linq.XElement i_RelatedXElement)
        {
            if (i_Sharing == null)
                throw new System.ArgumentException("i_Sharing must be non-null!");

            if (i_RelatedXElement == null)
                throw new System.ArgumentException("i_Related.XElement must be non-null!");

            bool InvalidXElement =
                (i_RelatedXElement.Name.LocalName != Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RELATED_ELEMENT_NAME) ||
                (i_RelatedXElement.Name.NamespaceName != Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.FEEDSYNC_XML_NAMESPACE_URI);

            if (InvalidXElement)
                throw new System.ArgumentException("Invalid i_RelatedXElement!");

            m_Sharing = i_Sharing;
            m_XElement = i_RelatedXElement;

            System.Xml.Linq.XAttribute XAttribute = i_RelatedXElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.TYPE_ATTRIBUTE);
            string RelatedType = XAttribute.Value;
            switch (RelatedType)
            {
                case Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RELATED_TYPE_AGGREGATED:
                    {
                        m_RelatedType = Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related.RelatedTypes.Aggregated;
                        break;
                    }

                case Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RELATED_TYPE_COMPLETE:
                    {
                        m_RelatedType = Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related.RelatedTypes.Complete;
                        break;
                    }

                default:
                    {
                        throw new System.ArgumentException("Unknown related node type: " + RelatedType);
                    }
            }

            XAttribute = i_RelatedXElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.LINK_ATTRIBUTE);
            if (XAttribute != null)
                m_Link = XAttribute.Value;

            XAttribute = i_RelatedXElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.TITLE_ATTRIBUTE);
            if (XAttribute != null)
                m_Title = XAttribute.Value;
        }

        public Related(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sharing i_Sharing, Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related.RelatedTypes i_RelatedType, string i_Link, string i_Title)
        {
            if (i_Sharing == null)
                throw new System.ArgumentException("i_Sharing must be non-null!");

            if (System.String.IsNullOrEmpty(i_Link))
                throw new System.ArgumentException("i_Link must be non-null!");

            string RelatedType = System.String.Empty;
            switch (m_RelatedType)
            {
                case Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related.RelatedTypes.Aggregated:
                    {
                        RelatedType = Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RELATED_TYPE_AGGREGATED;
                        break;
                    }

                case Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related.RelatedTypes.Complete:
                    {
                        RelatedType = Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RELATED_TYPE_COMPLETE;
                        break;
                    }

                default:
                    {
                        throw new System.ArgumentException("Unknown i_RelatedType!");
                    }
            }

            m_XElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.FeedSyncXNamespace + Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RELATED_ELEMENT_NAME);

            m_Sharing = i_Sharing;
            m_RelatedType = i_RelatedType;
            m_Link = i_Link;
            m_Title = i_Title;

            m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.TYPE_ATTRIBUTE, RelatedType);
            m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.LINK_ATTRIBUTE, m_Link);

            if (!System.String.IsNullOrEmpty(m_Title))
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.TITLE_ATTRIBUTE, i_Title);
        }

        public Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sharing Sharing
        {
            get
            {
                return m_Sharing;
            }
        }

        public System.Xml.Linq.XElement XElement
        {
            get
            {
                return m_XElement;
            }
        }

        public Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related.RelatedTypes RelatedType
        {
            get
            {
                return m_RelatedType;
            }
        }

        public string Link
        {
            get
            {
                return m_Link;
            }
        }

        public string Title
        {
            get
            {
                return m_Title;
            }
        }

    }
}
